
package com.scalar.chiptrack;

import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.utils.MessageDigestUtils;
import com.scalar.chiptrack.utils.StringUtils;


public final class MessageDigestUtilsAction extends ChipTrackAction {

    public ActionForward execute (ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) 
                                  throws Exception {
        String retval = "failure";
        LinkedHashMap errors = new LinkedHashMap();
        LinkedHashMap info = new LinkedHashMap();
        HttpSession session = request.getSession();
        MessageDigestUtilsForm messageDigestUtilsForm = (MessageDigestUtilsForm) form;
        String submitAction = request.getParameter ("submitAction");

        if ("Generate File".equalsIgnoreCase (submitAction) == true) {
            String fileName = messageDigestUtilsForm.getFileName();
            String dirLocation = messageDigestUtilsForm.getDirLocation();
            
            try {
                String keyFileLocation = MessageDigestUtils.generateKeyFile (dirLocation, fileName);
                info.put ("Secret Key has been generated and stored successfully at <b>" + keyFileLocation + "<b>", null);
                
                request.setAttribute (SessionKeys.INFO_MESSAGES, info);
                retval = "success";
            }
            catch (ScalarException se) {
                se.printStackTrace();
                errors.put ("Internal Server Error", "Could not generate secret key file");
                request.setAttribute (SessionKeys.ERROR_MESSAGES, errors);
            }
            
            return (mapping.findForward (retval));
        }
        
        if ("Get Message Digest".equalsIgnoreCase (submitAction) == true) {
            String secretKeyFile = messageDigestUtilsForm.getSecretKeyFile();
            String inputText = messageDigestUtilsForm.getInputText();
            
            try {
                String byteArray = StringUtils.concatBytes (MessageDigestUtils.getMAC (inputText, secretKeyFile), ",");
                MessageDigestUtilsForm mdUtilsForm = new MessageDigestUtilsForm();
                mdUtilsForm.setResultBytes (byteArray);
                System.out.println ("byte array = " + byteArray);
                request.setAttribute ("messageDigestUtilsForm", mdUtilsForm);
                retval = "success";
            }
            catch (ScalarException se) {
                se.printStackTrace();
                errors.put ("Internal Server Error", "Could not generate Message Digest");
                request.setAttribute (SessionKeys.ERROR_MESSAGES, errors);
            }
            
            return (mapping.findForward (retval));
        }
        
        return (mapping.findForward ("failure"));
    }
    
}